package down.sallai.ml.util.wx;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import down.sallai.ml.bean.WxTextMessageBean;
import down.sallai.ml.util.Constants;
import down.sallai.ml.util.okhttp.GetUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Date;
import java.util.Map;

/**
 * @description: $
 * @author: sallai
 * @time: 2022年1月5日 0005 下午 08:23:07 秒
 */
@Slf4j
@Component
public class MsgDispatcher {

    @Autowired
    GithubVerity githubVerity;

    @Autowired
    GetUtils getUtils;


    public String processMessage(Map<String, String> map) {
        String replyMsg = "";

        switch (map.get("MsgType")) {
            case MessageUtil.REQ_MESSAGE_TYPE_TEXT:
                log.info("处理用户文本消息");
                replyMsg = handlerTextMsg(map);
                break;
            case MessageUtil.REQ_MESSAGE_TYPE_IMAGE:
                log.info("处理用户图片消息");
                break;
            case MessageUtil.REQ_MESSAGE_TYPE_LINK:
                log.info("处理用户链接消息");
                break;
            case MessageUtil.REQ_MESSAGE_TYPE_LOCATION:
                log.info("处理用户位置消息");
                break;
            case MessageUtil.REQ_MESSAGE_TYPE_VIDEO:
                log.info("处理用户视频、小视频消息");
                break;
            case MessageUtil.REQ_MESSAGE_TYPE_VOICE:
                log.info("处理用户语音消息");
                break;
            default:
                log.error("未处理该用户消息类型->"+ map.get("MsgType"));
                replyMsg = "未定义该类型消息回复处理";
        }
        return replyMsg;
    }


    public String handlerTextMsg(Map<String, String> map) {
        log.info("handlerTextMsg enter");

        String jsonString = JSONObject.toJSONString(map);
        WxTextMessageBean textMessageRes = buildTextResponeseMsg(map);
        WxTextMessageBean textMessage = JSONObject.parseObject(jsonString, WxTextMessageBean.class);
        String msg = new String();
        switch (textMessage.getContent().toLowerCase()) {
            case "1":
            case "github":
                String code = githubVerity.setVerity();
                msg = code.equals("") ? "验证码:生成失败,请联系管理员" : code;
                break;
            default:
                try {
                    msg = autoReplyApi(textMessage.getContent());
                } catch (IOException e) {
                    e.printStackTrace();
                    msg = "出现异常喽~~";
                }
        }
        textMessageRes.setContent(msg);
        return MessageUtil.textMessageToXml(textMessageRes);
    }


    public WxTextMessageBean buildTextResponeseMsg(Map<String, String> map){
        String openid=map.get("FromUserName"); //用户openid
        String mpid=map.get("ToUserName");   //公众号原始ID
        WxTextMessageBean textMessageRes = new WxTextMessageBean();
        textMessageRes.setToUserName(openid);
        textMessageRes.setFromUserName(mpid);
        textMessageRes.setCreateTime(new Date().getTime());
        textMessageRes.setMsgType(MessageUtil.RESP_MESSAGE_TYPE_TEXT);
        return textMessageRes;
    }

    //auto reply message
    public String autoReplyApi(String str) throws IOException {
        String json = getUtils.sendGetRequest(Constants.WxReply.AUTO_REPLY_URL + str);
        String content = JSON.parseObject(json).getString("content");
        return content;
    }
}
